package com.dao.impl;

import com.bean.Order;
import com.bean.OrderItem;
import com.dao.OrderItemDao;

import java.util.List;
import java.util.Map;

public class OrderItemDaoImpl extends BaseDao implements OrderItemDao {

    @Override
    public int saveOrderItem(OrderItem orderItem) {
        String sql="insert into order_item(`name`,`count`,`price`,`totalprice`,`flowerid`,`enterpriseid`,`orderid`,`status`) values(?,?,?,?,?,?,?,?)";
        return update(sql,orderItem.getName(),orderItem.getCount(),orderItem.getPrice(),orderItem.getTotalprice(),orderItem.getFlowerid(),orderItem.getEnterpriseid(),orderItem.getOrderid(),orderItem.getStatus());
    }

    @Override
    public List<OrderItem> showMyOrderItem(String orderid) {
        String sql="select `id`,`name`,`count`,`price`,`totalprice`,`flowerid`,`enterpriseid`,`orderid`,`status` from order_item where orderid=?";
        return queryForList(OrderItem.class,sql,orderid);
    }

    @Override
    public OrderItem getOrderItem(Integer id) {
        String sql="select `id`,`name`,`count`,`price`,`totalprice`,`flowerid`,`enterpriseid`,`orderid`,`status` from order_item where id=?";
        return queryForOne(OrderItem.class,sql,id);
    }

    @Override
    public List<OrderItem> showMyOrderItems(String enterpriseid) {
        String sql="select `id`,`name`,`count`,`price`,`totalprice`,`flowerid`,`enterpriseid`,`orderid`,`status` from order_item where enterpriseid=?";
        return queryForList(OrderItem.class,sql,enterpriseid);
    }

    @Override
    public int updateOrderItem(Integer id) {
        String sql="update order_item set `status`=1 where id=?";
        return update(sql,id);
    }

    @Override
    public List<OrderItem> showOrderItemByStatus(Integer status) {
        String sql="select `id`,`name`,`count`,`price`,`totalprice`,`flowerid`,`enterpriseid`,`orderid`,`status` from order_item where status=?";
        return queryForList(OrderItem.class,sql,status);
    }

    @Override
    public int receiveOrderItem(Integer id) {
        String sql="update order_item set `status`=2 where id=?";
        return update(sql,id);
    }

    @Override
    public List<Map<String, Object>> showOrderItem(String orderid) {
        String sql="select * from order_item,enterprise where orderid=? and order_item.enterpriseid=enterprise.enterpriseid";
        return listquery(OrderItem.class,sql,orderid);
    }

    @Override
    public List<Map<String, Object>> queryUserid(String userid, Integer status) {
        String sql="select * from orders,order_item where orders.userid=? and order_item.status=? and orders.orderid=order_item.orderid group by order_item.id";
        return listquery(Order.class,sql,userid,status);
    }

    @Override
    public int updateStatus(Integer id) {
        String sql="update order_item set `status`=3 where id=?";
        return update(sql,id);
    }

    @Override
    public List<Map<String, Object>> queryStatus(Integer status, String enterpriseid) {
        String sql="select * from orders,order_item,user where order_item.status=? and order_item.enterpriseid=? and orders.userid=user.userid group by order_item.id";
        return listquery(OrderItem.class,sql,status,enterpriseid);
    }
}
